package com.yarin.android.Examples_08_09;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TabHost;
import java.util.Arrays;
import java.util.Map;
import java.util.HashMap;

public class Activity02 extends Activity implements TabHost.OnTabChangeListener {

   public LinearLayout mBottomControlPanel;
    private ImageButton volumeDownBtn;
    private ImageButton volumeUpBtn;
    private ImageButton powerBtn;
    private static CustomSpinner cSpinner;
    private ImageButton settingBtn;

    public static Activity02 galleryActivity;

    private ArrayAdapter<String> adapter;
    private static final String[] volumeArray = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
            "11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
            "21", "22", "23", "24", "25", "26", "27", "28", "29", "30",
            "31", "32", "33", "34", "35", "36", "37", "38", "39", "40"
    };
    public static int volumeValueFromRadio = 1;
    private static Map<String, Integer> positionVolumeMap;

    public static Handler handlerRadioInterface = new Handler() {
        public void handleMessage(Message msg) {//定义一个Handler，用于处理下载线程与UI间通讯
            if (!Thread.currentThread().isInterrupted()) {
                switch (msg.what) {
                    case 1: {
                        //int position = Arrays.binarySearch(volumeArray, volumeValueFromRadio + "");slow
                        int position = positionVolumeMap.get(volumeValueFromRadio+"");
                        cSpinner.setSelection(position);
                        Log.e("gallery", volumeValueFromRadio + "===int");
                        break;
                    }
                    case 2:
                        break;
                    case -1:
                        break;
                }
            }
            super.handleMessage(msg);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);

        galleryActivity = Activity02.this;

        TabHost mTabHost = (TabHost)findViewById(R.id.tabhost);
        mTabHost.setup();
        boolean flag;
        if(getResources().getConfiguration().orientation == 2)
            flag = true;
        else
            flag = false;
        if(flag)
            mTabHost.getTabWidget().setOrientation(1);
        mTabHost.addTab(mTabHost.newTabSpec("tab1")
                .setIndicator("tab1")
                .setContent(R.id.tab1));
        mTabHost.addTab(mTabHost.newTabSpec("tab1")
                .setIndicator("tab1")
                .setContent(R.id.tab2));
        mTabHost.addTab(mTabHost.newTabSpec("tab1")
                .setIndicator("tab1")
                .setContent(R.id.tab3));
        mTabHost.addTab(mTabHost.newTabSpec("tab1")
                .setIndicator("tab1")
                .setContent(R.id.tab4));
        mTabHost.addTab(mTabHost.newTabSpec("tab1")
                .setIndicator("tab1")
                .setContent(R.id.tab5));
        mTabHost.addTab(mTabHost.newTabSpec("tab1")
                .setIndicator("tab1")
                .setContent(R.id.tab6));
        mTabHost.setOnTabChangedListener(this);
        mBottomControlPanel = (LinearLayout)findViewById(R.id.bottomControlPanel)  ;
        volumeDownBtn = (ImageButton) mBottomControlPanel.findViewById(R.id.volumeDownBtn);
        volumeUpBtn = (ImageButton) mBottomControlPanel.findViewById(R.id.volumeUpBtn);
        volumeDownBtn.setOnClickListener(VolumeClickListener);
        volumeUpBtn.setOnClickListener(VolumeClickListener);

        powerBtn = (ImageButton) mBottomControlPanel.findViewById(R.id.powerBtn);
        powerBtn.setOnClickListener(VolumeClickListener);
        settingBtn = (ImageButton) mBottomControlPanel.findViewById(R.id.settingBtn);
        settingBtn.setOnClickListener(VolumeClickListener);

        cSpinner = (CustomSpinner)findViewById(R.id.volumeSpinner);
        adapter = new ArrayAdapter<String>(Activity02.galleryActivity, android.R.layout.simple_spinner_item, volumeArray);
        //设置下拉列表的风格
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //将adapter 添加到spinner中
        cSpinner.setAdapter(adapter);
        //添加事件Spinner事件监听
        cSpinner.setOnItemSelectedListener(new SpinnerSelectedListener());
        //设置默认值
        cSpinner.setVisibility(View.VISIBLE);

        positionVolumeMap = new HashMap<String, Integer>();
        for(int i=1; i<=40; i++) {
            positionVolumeMap.put(i+"", (i-1));
        }
    }

    View.OnClickListener VolumeClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int index = v.getId();
            switch (index) {
                case R.id.volumeDownBtn: {
                    Log.e("donghui","click R.id.volumeDownBtn");
                    byte[] targetOrder = { (byte)0xFF, (byte)0x77, (byte)0x03, (byte)0x01, (byte)0x02, (byte)0x00, (byte)0x00, (byte)0x0D, (byte)0x0A };
                    BluetoothDemo.sendBlueOrder(targetOrder);

                    cSpinner.setSelection(cSpinner.getSelectedItemPosition()-1);
                    break;
                }

                case R.id.volumeUpBtn: {
                    Log.e("donghui","click R.id.volumeUpBtn");
                    //FF7703010100000D0A
                    byte[] targetOrder = { (byte)0xFF, (byte)0x77, (byte)0x03, (byte)0x01, (byte)0x01, (byte)0x00, (byte)0x00, (byte)0x0D, (byte)0x0A };
                    BluetoothDemo.sendBlueOrder(targetOrder);

                    cSpinner.setSelection(cSpinner.getSelectedItemPosition()+1);
                    break;
                }

                case R.id.powerBtn: {
                    Log.e("gallery", "click R.id.volumeUpBtn");
                    //FF7703010100000D0A
                    byte[] targetOrder = { (byte)0xFF, (byte)0x77, (byte)0x02, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x03, (byte)0x0D, (byte)0x0A };
                    BluetoothDemo.sendBlueOrder(targetOrder);

                    //cSpinner.setSelection(cSpinner.getSelectedItemPosition()+1);
                    break;
                }

                case R.id.settingBtn: {
                    Log.e("gallery", "click R.id.settingBtn");
                    //FF7705000000000D0A
                    byte[] targetOrder = {(byte)0xFF, (byte)0x77, (byte)0x05, (byte)0x00, (byte)0x01, (byte)0x00,
                            (byte)0x00, (byte)0x0D, (byte)0x0A};
                    BluetoothDemo.sendBlueOrder(targetOrder);

                    break;
                }
            }
        }
    };

    @Override
    public void onTabChanged(String tabId) {
    }

    private void setAMProgress(int i)
    {
//        zl.android.caraudioremote.protocol.SPPInfoEnum.AMRegionInfo amregioninfo = model.getAMRegionInfo();
//        if(amregioninfo != null)
//        {
//            int j = FrequencyRangeUtil.GetAMProgress(amregioninfo, i);
//            LogUtil.v("TunerFragment", (new StringBuilder("setAMProgress progress ")).append(j).toString());
//            channelSeekBar.setProgress(j);
//            channelSeekBar.setProgressDrawable(getResources().getDrawable(amregioninfo.getResId()));
//            channelSeekBar.setThumb(getResources().getDrawable(0x7f0200e8));
//        }
    }

    class SpinnerSelectedListener implements AdapterView.OnItemSelectedListener {
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            System.out.println("gallery ===> " + volumeArray[arg2]);
        }

        public void onNothingSelected(AdapterView<?> arg0) {
        }
    }
}
